package org.javaboy.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.javaboy.ai.domain.Banner;
import org.javaboy.ai.mapper.BannerMapper;
import org.javaboy.ai.service.IBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author javaboy
 * @since 2024-07-03
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    @Autowired
    BannerMapper bannerMapper;

    @Override
    public List<Banner> getAllBanners() {
        LambdaQueryWrapper<Banner> qw = new LambdaQueryWrapper<>();
        qw.eq(Banner::getDelFlag, 1);
        qw.orderByAsc(Banner::getOrderNum);
        qw.select(Banner::getTitle, Banner::getImage, Banner::getUrl);
        return list(qw);
    }

    @Override
    public List<Banner> selectBannerList(Banner banner) {
        return bannerMapper.selectBannerList(banner);
    }

    @Override
    public boolean checkTitleExists(Banner banner) {
        LambdaQueryWrapper<Banner> qw = new LambdaQueryWrapper<>();
        qw.eq(Banner::getDelFlag, 1);
        qw.eq(Banner::getTitle, banner.getTitle());
        if (null != banner.getId()) {
            qw.ne(Banner::getId, banner.getId());
        }
        return exists(qw);
    }

    @Override
    public boolean deleteBanner(Long[] bannerIds) {
        LambdaUpdateWrapper<Banner> uw = new LambdaUpdateWrapper<>();
        uw.in(Banner::getId, bannerIds);
        uw.set(Banner::getDelFlag, 0);
        return update(uw);
    }
}
